from __future__ import annotations

import subprocess
import sys
from pathlib import Path


def run(cmd: list[str]) -> None:
    print("+", " ".join(cmd))
    subprocess.check_call(cmd)


def main() -> int:
    root = Path(__file__).resolve().parent
    os_cwd = Path.cwd()
    try:
        # Ensure we always run relative to the package root.
        import os

        os.chdir(root)

        run(
            [
                sys.executable,
                "scripts/prepare_owid_monthly.py",
                "--owid",
                "data/owid/owid-covid-data.csv",
                "--out",
                "outputs/data/owid_monthly.parquet",
                "--start",
                "2020-01-01",
                "--end",
                "2024-12-31",
            ]
        )
        run(
            [
                sys.executable,
                "scripts/prepare_vdem_baseline.py",
                "--vdem",
                "data/vdem/vdem.RData",
                "--out",
                "outputs/data/vdem_baseline.csv",
                "--baseline-year",
                "2019",
            ]
        )

        # Offline-first: use the included World Bank baseline snapshot if present.
        # If you want to refresh it via API, delete outputs/data/worldbank_baseline_2019.csv and call the script directly.
        out_wb = Path("outputs/data/worldbank_baseline_2019.csv")
        out_wb.parent.mkdir(parents=True, exist_ok=True)
        bundled_wb = Path("data/worldbank/worldbank_baseline_2019.csv")
        if not out_wb.exists() and bundled_wb.exists():
            out_wb.write_bytes(bundled_wb.read_bytes())

        if not out_wb.exists():
            run(
                [
                    sys.executable,
                    "scripts/download_worldbank_baseline.py",
                    "--baseline-year",
                    "2019",
                    "--min-year",
                    "2000",
                    "--out",
                    "outputs/data/worldbank_baseline_2019.csv",
                ]
            )
        run(
            [
                sys.executable,
                "scripts/build_merged_panel.py",
                "--owid-monthly",
                "outputs/data/owid_monthly.parquet",
                "--vdem-baseline",
                "outputs/data/vdem_baseline.csv",
                "--worldbank-baseline",
                "outputs/data/worldbank_baseline_2019.csv",
                "--out",
                "outputs/data/panel_merged.parquet",
                "--min-months",
                "18",
            ]
        )
        run(
            [
                sys.executable,
                "scripts/run_models_and_figures.py",
                "--panel",
                "outputs/data/panel_merged.parquet",
                "--out-dir",
                "outputs",
            ]
        )
        run(
            [
                sys.executable,
                "scripts/run_robustness.py",
                "--panel",
                "outputs/data/panel_merged.parquet",
                "--out-dir",
                "outputs",
                "--min-population",
                "1000000",
                "--min-months-nonmissing",
                "12",
            ]
        )
        run(
            [
                sys.executable,
                "scripts/make_submission_figures.py",
                "--panel",
                "outputs/data/panel_merged.parquet",
                "--out-dir",
                "submission_like",
            ]
        )
    finally:
        import os

        os.chdir(os_cwd)

    print("Done. Key figures:")
    print(" - submission_like/figures/fig1_panels.png")
    print(" - submission_like/figures/fig2_panels.png")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
